<template>
	<view>
		<cu-navbar :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">授权登录</block>
		</cu-navbar>
		<view class="cu-load load-modal">
			<view class="cuIcon-emojifill text-mauve"></view>
			<view class="gray-text">授权登录中...</view>
		</view>
	</view>
</template>

<script>
export default {
	onLoad(options) {
		this.order_id = options.order_id;
		this.state = this.$common.getQueryString('state');
		this.getCode();
	},
	data() {
		return {
			order_id: '',
			state: ''
		};
	},
	methods: {
		getCode() {
			var code = this.$common.getQueryString('code');
			if (code) {
				this.order_id ? this.getOpenId(code) : this.getAuthLogin(code);
			}
		},
		getOpenId(code) {
			let data = {
				code: code,
				scope: 2,
				state: this.state
			};
			//已登录接口
			this.$api.getopenid(data).then(res => {
				if (res.code) {
					this.$common.navigateTo(this.$ph.payment + `?order_id=${this.order_id}`);
				} else {
					this.$common.errorToShow(res.msg);
				}
			});
		},
		//未登录接口
		getAuthLogin(code) {
			let data = {
				code: code,
				scope: 1,
				state: this.state
			};
			this.$api.authLogin(data).then(res => {
				if (res.code == 1) {
					//没有绑定账号，跳转到绑定页面
					if (res.data == 100) {
						this.$common.errorToShow('请再次手机验证码登录',()=>{
							this.$common.navigateTo(this.$ph.login+'?auth=auth');
						})
					} else {
						//登陆成功，设置userinfo，并返回上一页
						this.$vuex('vuex_user', res.data.userinfo);
						uni.switchTab({ url: this.$ph.member });
					}
				} else {
					this.$common.errorToShow('登录失败，请重试');
				}
			});
		}
	}
};
</script>

<style>
page {
	background-color: #f4f6f8;
}
.cu-load.load-modal::after {
	border-left: 6upx solid #9c26b0;
}
</style>
